clear all;close all;clc

% fs = 96e3
fs = 32.1e3;
dur = 1;
% fsN = 44.1e3;
% load standard_hrir_database\subject_003\hrir_final.mat
[BB,fs2]=audioread('F_us_8k.wav');
fs2
BB = BB(1:fs2*1.5);
x_loc = 0:5:355;
out_hart = ncread(['Acoustic/Subject1/Subject1_HRIRs.sofa'],"Data.IR");
% out_hart = ncread(['Acoustic/Subject10/Subject10_BIRs.sofa'],"Data.IR");

x_loc = 0:5:355;
y_loc = [-57,-30,-15,0,15,30,45,60,75]
% for hori = 1:1:72
for hori = 72:-2:1
    x_loc(hori)
    for eli = 1:4:9
        % for Index = 1:1:648
        Index = (eli - 1)*72+hori;

        h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
        h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs

        h_l=resample(h_l,44.1e3, 32.1e3);
        h_r=resample(h_r,44.1e3, 32.1e3);
        % figure
        plot(h_l,'b');hold on;plot(h_r,'r')
        xlim([200 400])
        title(['Hori = ' num2str(x_loc(hori))  '  Vert = ' num2str(y_loc(eli))])


        %     BB = randn(1,round(dur*fs));
        fy(:,1) = conv(BB,h_l,'same');
        fy(:,2) = conv(BB,h_r,'same');

        sound(fy./2,fs2)
        pause(1.2*dur)
        % pause
        hold off;
    end


    %     Nfft = length(h_l);
    %     fsig = (1:Nfft)./Nfft.*fs;
end
